package com.evision.webcrawler.baidu.parser;

import com.evision.webcrawler.baidu.ImgDownloadUtil;
import com.evision.webcrawler.baidu.Parser;
import com.evision.webcrawler.baidu.RequestUrl;
import com.evision.webcrawler.baidu.url.BaiduUrl;
import com.evision.webcrawler.baidu.url.ImageInfoUrl;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.StatusLine;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;

/**
 * 
 * @author dell
 * 百度请求数据处理Parser
 * 处理百度接口获得请求数据,解析获得图像URL,并将图像放入图像请求队列中
 *
 */
public class BaiduJsonParser implements Parser{
	private static BaiduJsonParser instance;
	private String basePath;
	private BaiduJsonParser(String basePath){
		this.basePath = basePath;
	}
	public static BaiduJsonParser getInstance(String basePath){
		if(instance == null)
			instance = new BaiduJsonParser(basePath);
		return instance;
	}
	private final String parseStart = "["; 									//格式化数据字段起始标致 解析器将截取标致中间内容进行解析
	private final String parseEnd = "]";   									//格式化数据字段结束标致
	/**
	 * 百度Json Http请求接口解析
	 */
	public void parser(HttpResponse response, BlockingQueue<RequestUrl>[] outQueue,
			RequestUrl args) {
		BaiduUrl baiduUrl = (BaiduUrl)args;
		
		StatusLine statusLine = response.getStatusLine();
		if (statusLine.getStatusCode() >= 300) {
			return;
		}
		HttpEntity entity = response.getEntity();
		if(entity == null){
			return;
		}
		
		String content = null;
		try {
			content = new String(EntityUtils.toString(entity).getBytes("ISO-8859-1"),"UTF-8");;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		List list = new ArrayList();
		list.add("酒水饮料");
		list.add("结婚酒水 饮料 搭配");
		list.add("宴席酒水");
        list.add("宴会酒水点心");
        list.add("自助餐酒水");
        list.add("宴席酒水");
        list.add("酒店外建筑");

		String savepath = "";
		for (int i=0; i< list.size() ;i++) {
			if(baiduUrl.getUrl().contains(list.get(i).toString())) {
				savepath = "/home/boyum/img/" + list.get(i) + File.separator;

			}
		}
		
		if(content == null || content.length() == 0)return;
		int ps = content.indexOf(this.parseStart);
		if(ps == -1)return;
		content = content.substring(ps,content.length());
		int pe = content.lastIndexOf(this.parseEnd);
		if(pe == -1)return;
		content = content.substring(0,pe+1);
		try {	
			ObjectMapper mapper = new ObjectMapper();
			mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
			ImageInfoUrl[] imageList = mapper.readValue(content,ImageInfoUrl[].class);
			for(ImageInfoUrl iu : imageList){
				ImgDownloadUtil.imgDownload(iu.getUrl(),savepath);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
